package com.dq.service.statistics.impl;

import com.dq.controller.in.request.CustomerAnalysisQuery;
import com.dq.controller.in.request.DataCountRequest;
import com.dq.controller.in.request.StatisticsRequest;
import com.dq.domain.evaluation.EvaluationRecordTemplate;
import com.dq.domain.evaluation.EvaluationTemplate;
import com.dq.domain.evaluation.EvaluationTemplateChild;
import com.dq.domain.service.ServiceProject;
import com.dq.domain.statistics.ServiceProjectStatistics;
import com.dq.domain.statistics.SubscribeStatistics;
import com.dq.domain.subscribe.Subscribe;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.*;
import com.dq.repository.BaseRepositories;
import com.dq.utils.TimeUtil;
import com.dq.vo.ListVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.domain.orders.UserOrder;
import com.dq.domain.org.Org;
import com.dq.domain.record.ServiceRecord;
import com.dq.domain.statistics.Statistics;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.dto.*;
import com.dq.service.org.OrgService;
import com.dq.service.statistics.StatisticsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * @author liuhui
 * @date 2019/11/29 0029 16:59:43
 */
@Service
public class StatisticsServiceImpl extends BaseRepositories implements StatisticsService {
    @Autowired
    private OrgService orgService;
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Long sumStatisticsNumByTime(StatisticsRequest statisticsRequest, Org org) {
        StringBuilder hql = new StringBuilder("select  COALESCE(sum(u.number),0) from Statistics u where u.deleted=false");
        if(org != null){
            hql.append(" and u.org =:org");
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.org.id in ").append(orgIdsIn);
            }
        }
        hql.append(" and u.statisticsType=:statisticsType");
        if(null!=statisticsRequest.getBeginTime()){
            hql.append(" and u.countTime >=:beginTime");
        }
        if(null!=statisticsRequest.getEndTime()){
            hql.append(" and u.countTime <=:endTime");
        }
        Query q = this.entityManager.createQuery(hql.toString());
        q.setParameter("statisticsType",statisticsRequest.getStatisticsType());
        if(org != null){
            q.setParameter("org",org);
        }
        if(null!=statisticsRequest.getBeginTime()){
            q.setParameter("beginTime",statisticsRequest.getBeginTime());
        }
        if(null!=statisticsRequest.getEndTime()){
            q.setParameter("endTime",statisticsRequest.getEndTime());
        }
        return new Long(q.getSingleResult().toString());
    }

    @Override
    public Long sumLastStatisticsNumByTime(StatisticsRequest statisticsRequest, Org org) {
        StringBuilder hql = new StringBuilder("select COALESCE(sum(u.number),0) from Statistics u where u.deleted=false");
        if(org != null){
            hql.append(" and u.org =:org");
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.org.id in ").append(orgIdsIn);
            }
        }
        hql.append(" and u.statisticsType=:statisticsType");
        if(null!=statisticsRequest.getLastBeginTime()){
            hql.append(" and u.countTime >=:beginTime");
        }
        if(null!=statisticsRequest.getLastEndTime()){
            hql.append(" and u.countTime <=:endTime");
        }
        Query q = this.entityManager.createQuery(hql.toString());
        q.setParameter("statisticsType",statisticsRequest.getStatisticsType());
        if(org != null){
            q.setParameter("org",org);
        }
        if(null!=statisticsRequest.getLastBeginTime()){
            q.setParameter("beginTime",statisticsRequest.getLastBeginTime());
        }
        if(null!=statisticsRequest.getLastEndTime()){
            q.setParameter("endTime",statisticsRequest.getLastEndTime());
        }
        return new Long(q.getSingleResult().toString());
    }

    @Override
    public BigDecimal sumSalePrice(StatisticsRequest statisticsRequest) {
        StringBuilder hql = new StringBuilder("select COALESCE(sum(u.salePrice),0) from Statistics u where u.deleted=false");
        if(statisticsRequest.getOrgId()!=null){
            hql.append(" and u.org.id =:orgId");
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.org.id in ").append(orgIdsIn);
            }
        }
        if(StringUtils.isNotEmpty(statisticsRequest.getStatisticsType().toString())){
            hql.append(" and u.statisticsType=:statisticsType");
        }
        if(null!=statisticsRequest.getBeginTime()){
            hql.append(" and u.countTime >=:beginTime");
        }
        if(null!=statisticsRequest.getEndTime()){
            hql.append(" and u.countTime <=:endTime");
        }
        Query q = this.entityManager.createQuery(hql.toString());
        if(statisticsRequest.getOrgId()!=null){
            q.setParameter("orgId",statisticsRequest.getOrgId());
        }
        if(StringUtils.isNotEmpty(statisticsRequest.getStatisticsType().toString())){
            q.setParameter("statisticsType",statisticsRequest.getStatisticsType());
        }
        if(null!=statisticsRequest.getBeginTime()){
            q.setParameter("beginTime",statisticsRequest.getBeginTime());
        }
        if(null!=statisticsRequest.getEndTime()){
            q.setParameter("endTime",statisticsRequest.getEndTime());
        }
        return (BigDecimal) q.getSingleResult();
    }

    @Override
    public BigDecimal sumLastSalePrice(StatisticsRequest statisticsRequest) {
        StringBuilder hql = new StringBuilder("select COALESCE(sum(u.salePrice),0) from Statistics u where u.deleted=false");
        if(statisticsRequest.getOrgId()!=null){
            hql.append(" and u.org.id =:orgId");
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.org.id in ").append(orgIdsIn);
            }
        }
        if(StringUtils.isNotEmpty(statisticsRequest.getStatisticsType().toString())){
            hql.append(" and u.statisticsType=:statisticsType");
        }
        if(null!=statisticsRequest.getLastBeginTime()){
            hql.append(" and u.countTime >=:beginTime");
        }
        if(null!=statisticsRequest.getLastEndTime()){
            hql.append(" and u.countTime <=:endTime");
        }
        Query q = this.entityManager.createQuery(hql.toString());
        if(statisticsRequest.getOrgId()!=null){
            q.setParameter("orgId",statisticsRequest.getOrgId());
        }
        if(StringUtils.isNotEmpty(statisticsRequest.getStatisticsType().toString())){
            q.setParameter("statisticsType",statisticsRequest.getStatisticsType());
        }
        if(null!=statisticsRequest.getLastBeginTime()){
            q.setParameter("beginTime",statisticsRequest.getLastBeginTime());
        }
        if(null!=statisticsRequest.getLastEndTime()){
            q.setParameter("endTime",statisticsRequest.getLastEndTime());
        }
        return (BigDecimal) q.getSingleResult();
    }

    @Override
    public ListVo list(DataCountRequest dataCountRequest) throws AdminException {
        Map<String, Object> query = dataCountRequest.parseMap();
        query.put("page",0);
        query.put("size",dataCountRequest.getNum());
        if(dataCountRequest.getOrgId() != null){
            query.put("org_id",dataCountRequest.getOrgId());
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            query.put("org" + BaseDaoImpl.inSqlStr, orgList);
        }
       /* if(StringUtils.isNotEmpty(dataCountRequest.getStatisticsType().toString())){
            query.put("statisticsType",)
        }*/
       query.remove("num");
        List<Statistics> statistics = dao.find(dao.query2Hql(Statistics.class,query),query);
        Long count = dao.count(dao.count2Hql(Statistics.class,query),query);
        List<Statistics> statisticsList = new ArrayList<>();
        if(null!=statistics && statistics.size()>0){
            statisticsList = statistics;
        }
        return new ListVo((List<Statistics.Vo>) VO.toVOCollection(statisticsList));
    }

    @Override
    public Long active(StatisticsRequest statisticsRequest) {
        StringBuilder hql = new StringBuilder("select COALESCE(count(id),0) from WxUser u where u.deleted=false");
        hql.append(" and u.customerType=:customerType");
        if(statisticsRequest.getOrgId() != null){
            hql.append(" and u.org.id=:orgId");
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.org.id in ").append(orgIdsIn);
            }
        }
        Query q = this.entityManager.createQuery(hql.toString());
        q.setParameter("customerType", WxUser.CustomerType.active);
        if(statisticsRequest.getOrgId() != null){
            q.setParameter("orgId",statisticsRequest.getOrgId());
        }
        return (Long) q.getSingleResult();
    }

    @Override
    public Long sum(StatisticsRequest statisticsRequest) {
        StringBuilder hql = new StringBuilder("select COALESCE(count(id),0) from WxUser u where u.deleted=false");
        if(statisticsRequest.getOrgId() != null){
            hql.append(" and u.org.id=:orgId");
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.org.id in ").append(orgIdsIn);
            }
        }
        Query q = this.entityManager.createQuery(hql.toString());
        if(statisticsRequest.getOrgId() != null){
            q.setParameter("orgId",statisticsRequest.getOrgId());
        }
        return (Long) q.getSingleResult();
    }

    @Override
    public Vo userOrderList(CustomerAnalysisQuery customerAnalysisQuery) throws AdminException {
        customerAnalysisQuery.format();
        Map<String, Object> query = customerAnalysisQuery.parseMap();
        if(customerAnalysisQuery.getOrgId() != null){
            query.put("org_id",customerAnalysisQuery.getOrgId());
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            query.put("org" + BaseDaoImpl.inSqlStr, orgList);
        }
        if(StringUtils.isNotEmpty(customerAnalysisQuery.getCustomerType().toString())){
            query.put("wxUser_customerType",customerAnalysisQuery.getCustomerType());
            query.remove("customerType");
        }
        if(customerAnalysisQuery.getQueryTimestamp() != null){
            if(customerAnalysisQuery.getCustomerType().equals(WxUser.CustomerType.active)){
                query.put("createTime"+ BaseDaoImpl.gteSqlStr, customerAnalysisQuery.getQueryTimestamp());
                query.remove("queryTimestamp");
            }else {
                query.put("createTime"+ BaseDaoImpl.lteSqlStr, customerAnalysisQuery.getQueryTimestamp());
                query.remove("queryTimestamp");
            }
        }
        List<UserOrder> userOrders = dao.find(dao.query2Hql(UserOrder.class,query),query);
        Long count = dao.count(dao.count2Hql(UserOrder.class,query),query);
        List<WxUser.SimpleWxUser> simpleWxUsers =new ArrayList<>();
        userOrders.forEach(userOrder -> {
            WxUser wxUser = (WxUser) userOrder.getWxUser();
            WxUser.SimpleWxUser simpleWxUser =wxUser.toSimpleWxUser(userOrder.getOrg().getSimpleName());
            simpleWxUsers.add(simpleWxUser);
        });
        PageInfo pageInfo = new PageInfo(simpleWxUsers);
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }

    @Override
    public Long orgRanking(Long beginTime,Long endTime,Long orgId,Statistics.StatisticsType statisticsType){
        StringBuilder hql = new StringBuilder("select COALESCE(sum(u.number),0) from Statistics u where u.deleted=false" +
                " and u.countTime >=:beginTime and u.countTime <=:endTime and u.org.id=:orgId and u.statisticsType =:statisticsType");
        Query q = this.entityManager.createQuery(hql.toString());
        if(orgId != null){
            q.setParameter("orgId",orgId);
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.org.id in ").append(orgIdsIn);
            }
        }
        if(beginTime != null){
            q.setParameter("beginTime",beginTime);
        }
        if(endTime != null){
            q.setParameter("endTime",endTime);
        }
        if(StringUtils.isNotEmpty(statisticsType.toString())){
            q.setParameter("statisticsType",statisticsType);
        }
        return (Long) q.getSingleResult();
    }

    @Override
    public HashMap<String,Object> countCustomer(CountCustomerQuery countCustomerQuery) {
        List<WxUser.Vo> activeUser = new ArrayList<>();
        List<WxUser.Vo> silentUser = new ArrayList<>();
        HashMap<String,Object> map = new HashMap<>();
        Org org = dao.get(Org.class,countCustomerQuery.getOrgId());
        List<WxUser> wxUserList = wxUserRepository.findByDeletedFalse();
        wxUserList.forEach(wxUser -> {
            if(serviceRecordRepository.findByWxUserAndSysDeptAndServiceStatusAndCreateTimeLessThanEqualAndCreateTimeGreaterThanEqualAndDeletedFalse(wxUser,org ,ServiceRecord.ServiceStatus.endService,countCustomerQuery.getBeginTime(),countCustomerQuery.getEndTime()).size()>0){
                activeUser.add((WxUser.Vo) wxUser.toVO());
            }else {
                silentUser.add((WxUser.Vo) wxUser.toVO());
            }
        });
        map.put("activeUser",activeUser);
        map.put("activeUserNum",activeUser.size());
        map.put("silentUser",silentUser);
        map.put("silentUserNum",silentUser.size());
        return map;
    }

    @Override
    public List<StaffManualPriceDto> countManualPrice(StaffManualPriceQuery staffManualPriceQuery) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COALESCE(sum(sr.manual_price),0) manualPrice,su.id staffUserId,su.user_realc_name  userRealcName FROM `service_record` sr \n" +
                "LEFT JOIN sys_user su \n" +
                "on sr.staff_user_id = su.id ");
        sql = getWhereSql(staffManualPriceQuery,sql);
        sql.append(" GROUP BY sr.staff_user_id ");
        sql.append(" ORDER BY sr.manual_price desc");

        System.out.println(sql);

        // 设置动态参数
        Map<String, Object> query = new HashMap<>(16);
        if (staffManualPriceQuery.getPage() != null && staffManualPriceQuery.getSize() != null) {
            query.put("size", staffManualPriceQuery.getSize());
            query.put("page", staffManualPriceQuery.getPage());
        }
        List<Map> list = dao.findBySql2Map(sql.toString(), query);

        List<StaffManualPriceDto> staffManualPriceDtos = new ArrayList<>();

        for(Map map : list){
            StaffManualPriceDto dto = new StaffManualPriceDto();
            dto.setManualPrice((BigDecimal) map.get("manualPrice"));
            dto.setStaffUserId(Long.parseLong(map.get("staffUserId").toString()));
            dto.setUserRealcName(map.get("userRealcName").toString());
            staffManualPriceDtos.add(dto);
        }
        return staffManualPriceDtos;
    }

    @Override
    public StringBuilder getWhereSql(StaffManualPriceQuery staffManualPriceQuery,StringBuilder sql) {
        sql.append(" where sr.deleted = false ");
        sql.append(" and sr.service_status = 1 ");
        if(staffManualPriceQuery.getBeginTime() != null){
            sql.append(" and sr.create_time >= ").append(staffManualPriceQuery.getBeginTime());
        }
        if(staffManualPriceQuery.getEndTime() != null){
            sql.append(" and sr.create_time <= ").append(staffManualPriceQuery.getEndTime());
        }
        if(staffManualPriceQuery.getOrgId() != null){
            sql.append(" and sr.sys_dept_id=  ").append(staffManualPriceQuery.getOrgId());
        }
        return sql;
    }

    @Override
    public List<WagesStatisticeDto> countWages(StaffManualPriceQuery staffManualPriceQuery) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COALESCE(sum(pr.total_amount),0) totalAmount,su.id staffUserId,su.user_realc_name  userRealcName FROM `payroll_record` pr \n" +
                " LEFT JOIN sys_user su \n" +
                " on pr.staff_user_id = su.id " +
                " where pr.deleted = false and pr.payroll_status=2");
        if(staffManualPriceQuery.getBeginTime() != null){
            sql.append(" and pr.create_time >= ").append(staffManualPriceQuery.getBeginTime());
        }
        if(staffManualPriceQuery.getEndTime() != null){
            sql.append(" and pr.create_time <= ").append(staffManualPriceQuery.getEndTime());
        }
        if(staffManualPriceQuery.getOrgId() != null){
            sql.append(" and pr.org_id=  ").append(staffManualPriceQuery.getOrgId());
        }
        sql.append(" GROUP BY pr.staff_user_id  ");
        sql.append(" ORDER BY pr.total_amount desc");

        System.out.println(sql);

        Map<String, Object> query = new HashMap<>(16);
        if (staffManualPriceQuery.getPage() != null && staffManualPriceQuery.getSize() != null) {
            query.put("size", staffManualPriceQuery.getSize());
            query.put("page", staffManualPriceQuery.getPage());
        }
        List<Map> list = dao.findBySql2Map(sql.toString(), query);

        List<WagesStatisticeDto> wagesStatisticeDtos = new ArrayList<>();
        for(Map m : list){
            WagesStatisticeDto wagesStatisticeDto = new WagesStatisticeDto();
            wagesStatisticeDto.setStaffUserId(Long.parseLong(m.get("staffUserId").toString()));
            wagesStatisticeDto.setTotalAmount((BigDecimal) m.get("totalAmount"));
            wagesStatisticeDto.setUserRealcName(m.get("userRealcName").toString());
            wagesStatisticeDtos.add(wagesStatisticeDto);
        }
        return wagesStatisticeDtos;
    }

    @Override
    public List<RewardStatisticeDto> countReward(StaffManualPriceQuery staffManualPriceQuery) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COALESCE(sum(re.reward_price),0) rewardPrice,su.id staffUserId,su.user_realc_name  userRealcName FROM `reward` re \n" +
                "LEFT JOIN sys_user su \n" +
                "on re.staff_user_id = su.id ");
        if(staffManualPriceQuery.getBeginTime() != null){
            sql.append(" and re.create_time >= ").append(staffManualPriceQuery.getBeginTime());
        }
        if(staffManualPriceQuery.getEndTime() != null){
            sql.append(" and re.create_time <= ").append(staffManualPriceQuery.getEndTime());
        }
        if(staffManualPriceQuery.getOrgId() != null){
            sql.append(" and re.org_id=  ").append(staffManualPriceQuery.getOrgId());
        }
        sql.append(" GROUP BY re.staff_user_id   ");
        sql.append(" ORDER BY re.reward_price desc");

        System.out.println(sql);

        Map<String, Object> query = new HashMap<>(16);
        if (staffManualPriceQuery.getPage() != null && staffManualPriceQuery.getSize() != null) {
            query.put("size", staffManualPriceQuery.getSize());
            query.put("page", staffManualPriceQuery.getPage());
        }
        List<Map> list = dao.findBySql2Map(sql.toString(), query);

        List<RewardStatisticeDto> rewardStatisticeDtos = new ArrayList<>();
        for(Map m : list){
            RewardStatisticeDto rewardStatisticeDto = new RewardStatisticeDto();
            rewardStatisticeDto.setRewardPrice((BigDecimal) m.get("rewardPrice"));
            rewardStatisticeDto.setStaffUserId(Long.parseLong(m.get("staffUserId").toString()));
            rewardStatisticeDto.setUserRealcName(m.get("userRealcName").toString());
            rewardStatisticeDtos.add(rewardStatisticeDto);
        }
        return rewardStatisticeDtos;
    }

    @Override
    public List<StaffStatisticeDto> countStaffRank(staffStatisticeQuery staffManualPriceQuery) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COALESCE(count(*),0) sum,st.user_realc_name staffUserRealcName,ss.simple_name orgName\n" +
                "FROM `sys_user` wx LEFT JOIN sys_user st\n" +
                "ON wx.staff_user_id = st.id\n" +
                "LEFT JOIN sys_section ss\n" +
                "on wx.section_id = ss.id");
        sql.append(" where  wx.dtype='WxUser' and wx.deleted=0 and wx.member_type=1 ");
        if(staffManualPriceQuery.getBeginTime() != null){
            sql.append(" and wx.create_time >= ").append(staffManualPriceQuery.getBeginTime());
        }
        if(staffManualPriceQuery.getEndTime() != null){
            sql.append(" and wx.create_time <= ").append(staffManualPriceQuery.getEndTime());
        }
        if(staffManualPriceQuery.getOrgId() != null){
            sql.append(" and wx.section_id=  ").append(staffManualPriceQuery.getOrgId());
        }
        if(staffManualPriceQuery.getFlag() == 1){
            sql.append(" GROUP BY wx.staff_user_id   ");
        }else if(staffManualPriceQuery.getFlag() == 2){
            sql.append(" GROUP BY wx.section_id  ");
        }
        sql.append(" ORDER BY sum asc ");

        System.out.println(sql);

        Map<String, Object> query = new HashMap<>(16);
        if (staffManualPriceQuery.getPage() != null && staffManualPriceQuery.getSize() != null) {
            query.put("size", staffManualPriceQuery.getSize());
            query.put("page", staffManualPriceQuery.getPage());
        }
        List<Map> list = dao.findBySql2Map(sql.toString(), query);

        List<StaffStatisticeDto> staffStatisticeDtos = new ArrayList<>();

        for (Map m :list){
            StaffStatisticeDto staffStatisticeDto = new StaffStatisticeDto();
            staffStatisticeDto.setStaffUserRealcName(m.get("staffUserRealcName").toString());
            staffStatisticeDto.setSum(Long.parseLong(m.get("sum").toString()));
            staffStatisticeDto.setOrgName(m.get("orgName").toString());
            staffStatisticeDtos.add(staffStatisticeDto);
        }
        return staffStatisticeDtos;
    }

    @Override
    public List<CustomerStatisticeDto> countCustomerStatistice(CustomerCountQuery customerCountQuery) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COALESCE(count(*),0) sum FROM `service_record` ");
        sql.append(" where deleted=0 and service_status = 1 ");
        if(customerCountQuery.getBeginTime() != null){
            sql.append(" and create_time <= ").append(customerCountQuery.getBeginTime());
        }
        if(customerCountQuery.getEndTime() != null){
            sql.append(" and create_time >= ").append(customerCountQuery.getEndTime());
        }
        if(customerCountQuery.getOrgId() != null){
            sql.append(" and sys_dept_id = ").append(customerCountQuery.getOrgId());
        }
        if(customerCountQuery.getStaffUserId() != null){
            sql.append(" and staff_user_id = ").append(customerCountQuery.getStaffUserId());
        }
        System.out.println(sql);

        Map<String, Object> query = new HashMap<>(16);
        if (customerCountQuery.getPage() != null && customerCountQuery.getSize() != null) {
            query.put("size", customerCountQuery.getSize());
            query.put("page", customerCountQuery.getPage());
        }
        List<Map> list = dao.findBySql2Map(sql.toString(), query);

        List<CustomerStatisticeDto> customerStatisticeDtoList = new ArrayList<>();

        for(Map m : list){
            CustomerStatisticeDto customerStatisticeDto = new CustomerStatisticeDto();
            customerStatisticeDto.setSum(Long.parseLong(m.get("sum").toString()));
            customerStatisticeDtoList.add(customerStatisticeDto);
        }

        return customerStatisticeDtoList;
    }

    @Override
    public List<ServiceProjectStatisticeDto> countServiceProject(ServiceProjectStatisticsQuery serviceProjectStatisticsQuery) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COALESCE(count(*),0) sum,sp.project_name projectName FROM `service_project_record_centre` sprc LEFT JOIN service_project sp\n" +
                "on sprc.service_project_id = sp.id\n" +
                "left JOIN service_record sr \n" +
                "on sprc.service_record_id = sr.id");
        sql.append(" where sprc.deleted=0 and sr.service_status = 1 ");
        if(serviceProjectStatisticsQuery.getBeginTime() != null){
            sql.append(" and sprc.create_time <= ").append(serviceProjectStatisticsQuery.getBeginTime());
        }
        if(serviceProjectStatisticsQuery.getEndTime() != null){
            sql.append(" and sprc.create_time >= ").append(serviceProjectStatisticsQuery.getEndTime());
        }
        sql.append(" GROUP BY sprc.service_project_id ");
        sql.append(" ORDER BY sum DESC ");

        System.out.println(sql);

        Map<String, Object> query = new HashMap<>(16);
        if (serviceProjectStatisticsQuery.getPage() != null && serviceProjectStatisticsQuery.getSize() != null) {
            query.put("size", serviceProjectStatisticsQuery.getSize());
            query.put("page", serviceProjectStatisticsQuery.getPage());
        }
        List<Map> list = dao.findBySql2Map(sql.toString(), query);

        List<ServiceProjectStatisticeDto> serviceProjectStatisticeDtoList = new ArrayList<>();
        for(Map m : list) {
            ServiceProjectStatisticeDto serviceProjectStatisticeDto = new ServiceProjectStatisticeDto();
            serviceProjectStatisticeDto.setSum(Long.parseLong(m.get("sum").toString()));
            serviceProjectStatisticeDto.setProjectName(m.get("projectName").toString());
            serviceProjectStatisticeDtoList.add(serviceProjectStatisticeDto);
        }

        return serviceProjectStatisticeDtoList;
    }

    @Override
    public List<SaleStatisticeDto> countSale(SaleStatisticsQuery saleStatisticsQuery, boolean isoOrg) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COALESCE(SUM(top_price),0) price,COALESCE(SUM(expend_price),0) expendPrice,wx_user_id wxUserId,org_id orgId FROM `sale_statistics`" +
                "  where deleted=false ");

        List<Long> orgidsInList = new ArrayList<>();
        if(saleStatisticsQuery.getOrgId() != null) {
            orgidsInList.add(saleStatisticsQuery.getOrgId());
        }else {
            if(isoOrg) {
                // 只显示负责门店信息
                List<Org> orgList = orgService.getListAllByUser();
                for (Org org : orgList) {
                    orgidsInList.add(org.getId());
                }
            }else {
                // 所有店铺
            }
        }
        if(orgidsInList.size() > 0) {
            StringBuilder s = new StringBuilder();
            for (Long aLong : orgidsInList) {
                s.append(aLong).append(",");
            }
            sql.append(String.format(" and org_id in (%s)", s.toString().substring(0, s.toString().length()-1)));
        }

        if(saleStatisticsQuery.getBeginTime() != null){
            sql.append(" and create_time >=").append(saleStatisticsQuery.getBeginTime());
        }
        if(saleStatisticsQuery.getEndTime() != null){
            sql.append(" and create_time <=").append(saleStatisticsQuery.getEndTime());
        }
        if(saleStatisticsQuery.getWxUserId() != null){
            sql.append(" and wx_user_id =").append(saleStatisticsQuery.getWxUserId());
        }
        sql.append(" GROUP BY wx_user_id");
        sql.append(" ORDER BY price desc");
        Map<String, Object> query = new HashMap<>(16);
        if (saleStatisticsQuery.getPage() != null && saleStatisticsQuery.getSize() != null) {
            query.put("size", saleStatisticsQuery.getSize());
            query.put("page", saleStatisticsQuery.getPage());
        }
        List<Map> list = dao.findBySql2Map(sql.toString(), query);

        List<SaleStatisticeDto> saleStatisticeDtoList = new ArrayList<>();
        for(Map m : list) {
            SaleStatisticeDto saleStatisticeDto = new SaleStatisticeDto();
            BigDecimal price = BigDecimal.ZERO;
            if(m.get("price") != null) {
                if(StringUtils.isNotBlank(m.get("price").toString())) {
                    price = (BigDecimal) m.get("price");
                }
            }
            saleStatisticeDto.setTopPrice(price);
            BigDecimal expendPrice = BigDecimal.ZERO;
            if(m.get("expendPrice") != null) {
                if(StringUtils.isNotBlank(m.get("expendPrice").toString())) {
                    expendPrice = (BigDecimal) m.get("expendPrice");
                }
            }
            saleStatisticeDto.setExpendPrice(expendPrice);
            saleStatisticeDto.setPrice(saleStatisticeDto.getTopPrice().add(saleStatisticeDto.getExpendPrice()));

            if(m.get("wxUserId") != null) {
                if(StringUtils.isNotBlank(m.get("wxUserId").toString())) {
                    saleStatisticeDto.setWxUserId(Long.parseLong(m.get("wxUserId").toString()));
                    WxUser wxUser = dao.get(WxUser.class,Long.parseLong(m.get("wxUserId").toString()));
                    if(wxUser != null){
                        saleStatisticeDto.setWxUserRealcName(wxUser.getUserRealcName());
                    }
                }
            }
            if(m.get("orgId") != null) {
                if(StringUtils.isNotBlank(m.get("orgId").toString())) {
                    saleStatisticeDto.setOrgId(Long.parseLong(m.get("orgId").toString()));
                    Org org = dao.get(Org.class,Long.parseLong(m.get("orgId").toString()));
                    if(org != null){
                        saleStatisticeDto.setOrgName(org.getSimpleName());
                    }
                }
            }
            saleStatisticeDtoList.add(saleStatisticeDto);
        }
        return saleStatisticeDtoList;
    }

    @Override
    public Long achievementCount(CountAchievementQuery countAchievementQuery) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COALESCE(SUM(sale_price),0) price,COALESCE(SUM(recharge_price),0) rechargePrice,\n" +
                "COALESCE(SUM(consume_price),0) consumePrice,staff_user_id staffUserId,org_id orgId, subscribe_num subscribeNum, wxuser_num wxuserNum FROM `achievement_statistics` where deleted=false");

        achievementSql(countAchievementQuery, sql);

        List<Map> list = dao.findBySql2Map(sql.toString(), new HashMap<>(16));

        Long count = 0L;
        if(list != null) {
            count = Long.parseLong(String.valueOf(list.size()));
        }
        return count;
    }
    @Override
    public List<AchievementStatisticsDto> achievementList(CountAchievementQuery countAchievementQuery) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT staff_user_id staffUserId,org_id orgId,create_time createTime, ");
        if(countAchievementQuery.getFlag() == 1){
            sql.append(" COALESCE(SUM(sale_price),0) price,COALESCE(SUM(recharge_price),0) rechargePrice,COALESCE(SUM(consume_price),0) consumePrice,SUM(subscribe_num) subscribeNum ,SUM(subscribe_finish_num) subscribeFinishNum");
        }else {
            sql.append(" sale_price price,recharge_price rechargePrice,consume_price consumePrice,subscribe_num subscribeNum ,subscribe_finish_num subscribeFinishNum");
        }
        sql.append(" FROM `achievement_statistics` where deleted=false");
        achievementSql(countAchievementQuery, sql);

        Map<String, Object> query = new HashMap<>(16);
        if (countAchievementQuery.getPage() != null && countAchievementQuery.getSize() != null) {
            query.put("size", countAchievementQuery.getSize());
            query.put("page", countAchievementQuery.getPage());
        }

        List<Map> list = dao.findBySql2Map(sql.toString(), query);

        List<AchievementStatisticsDto> achievementStatisticsDtos = new ArrayList<>();
        for(Map m : list) {
            AchievementStatisticsDto achievementStatisticsDto = new AchievementStatisticsDto();
            BigDecimal price = BigDecimal.ZERO;
            BigDecimal rechargePrice = BigDecimal.ZERO;
            BigDecimal consumePrice = BigDecimal.ZERO;
            Long subscribeNum = 0L;
            Long subscribeFinishNum = 0L;
            Long wxuserNum = 0L;

            if(m.get("price") != null && StringUtils.isNotBlank(m.get("price").toString())){
                price = (BigDecimal) m.get("price");
            }
            if(m.get("rechargePrice") != null && StringUtils.isNotBlank(m.get("rechargePrice").toString())){
                rechargePrice = (BigDecimal) m.get("rechargePrice");
            }
            if(m.get("consumePrice") != null && StringUtils.isNotBlank(m.get("consumePrice").toString())){
                consumePrice = (BigDecimal) m.get("consumePrice");
            }
            if(m.get("subscribeNum") != null && StringUtils.isNotBlank(m.get("subscribeNum").toString())){
                subscribeNum = Long.parseLong(m.get("subscribeNum").toString());
            }
            if(m.get("subscribeFinishNum") != null && StringUtils.isNotBlank(m.get("subscribeFinishNum").toString())){
                subscribeFinishNum = Long.parseLong(m.get("subscribeFinishNum").toString());
            }
            if(m.get("wxuserNum") != null && StringUtils.isNotBlank(m.get("wxuserNum").toString())){
                wxuserNum = (Long) m.get("wxuserNum");
            }
            if(m.get("createTime") != null && StringUtils.isNotBlank(m.get("createTime").toString())){
                achievementStatisticsDto.setCreateTime(TimeUtil.longToString(Long.parseLong(m.get("createTime").toString()),TimeUtil.MMDD));
            }
            achievementStatisticsDto.setSalePrice(price);
            achievementStatisticsDto.setRechargePrice(rechargePrice);
            achievementStatisticsDto.setConsumePrice(consumePrice);
            achievementStatisticsDto.setSubscribeNum(subscribeNum);
            achievementStatisticsDto.setSubscribeFinishNum(subscribeFinishNum);
            achievementStatisticsDto.setWxuserNum(wxuserNum);
            if(m.get("staffUserId") != null && StringUtils.isNotBlank(m.get("staffUserId").toString())){
                achievementStatisticsDto.setStaffUserId(Long.parseLong(m.get("staffUserId").toString()));
//                bossUser = dao.get(BossUser.class, achievementStatisticsDto.getStaffUserId());
//                if(bossUser != null){
//                    achievementStatisticsDto.setStaffUserRealcName(bossUser.getUserRealcName());
//                }
            }
            if(m.get("orgId") != null && StringUtils.isNotBlank(m.get("orgId").toString())){
                achievementStatisticsDto.setOrgId(Long.parseLong(m.get("orgId").toString()));
//                Org org = dao.get(Org.class, achievementStatisticsDto.getOrgId());
//                if(org != null){
//                    achievementStatisticsDto.setOrgName(org.getSimpleName());
//                }
            }
            if(m.get("departmentId") != null && StringUtils.isNotBlank(m.get("departmentId").toString())){
                achievementStatisticsDto.setDepartmentId(Long.parseLong(m.get("departmentId").toString()));
            }
            if(m.get("provincialSectionId") != null && StringUtils.isNotBlank(m.get("provincialSectionId").toString())){
                achievementStatisticsDto.setProvincialSectionId(Long.parseLong(m.get("provincialSectionId").toString()));
            }
            if(countAchievementQuery.getType() != null){
                switch(countAchievementQuery.getType()){
                    case "2" :
                        if(achievementStatisticsDto.getOrgId() != null) {
                            Org org = dao.get(Org.class, achievementStatisticsDto.getOrgId());
                            if(org != null) {
                                achievementStatisticsDto.setName(org.getSimpleName());
                            }
                        }
                        break;
                    case "3" :
                        if(achievementStatisticsDto.getDepartmentId() != null) {
                            SysSection section = dao.get(SysSection.class, achievementStatisticsDto.getDepartmentId());
                            if(section != null) {
                                achievementStatisticsDto.setName(section.getSimpleName());
                            }
                        }
                        break;
                    case "4" :
                        if(achievementStatisticsDto.getProvincialSectionId() != null) {
                            SysSection section = dao.get(SysSection.class, achievementStatisticsDto.getProvincialSectionId());
                            if(section != null) {
                                achievementStatisticsDto.setName(section.getSimpleName());
                            }
                        }
                        break;
                    default :
                        if(achievementStatisticsDto.getStaffUserId() != null) {
                            BossUser bossUser = dao.get(BossUser.class, achievementStatisticsDto.getStaffUserId());
                            if(bossUser != null) {
                                achievementStatisticsDto.setName(bossUser.getUserRealcName());
                            }
                        }
                }
            }

            achievementStatisticsDtos.add(achievementStatisticsDto);
        }
        return achievementStatisticsDtos;
    }

    private void achievementSql(CountAchievementQuery countAchievementQuery, StringBuilder sql) {
        if(countAchievementQuery.getOrgId() != null){
            sql.append(" and org_id=").append(countAchievementQuery.getOrgId());
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                sql.append(" and org_id in ").append(orgIdsIn);
            }
        }

        if(countAchievementQuery.getBeginTime() != null){
            sql.append(" and create_time >=").append(countAchievementQuery.getBeginTime());
        }
        if(countAchievementQuery.getEndTime() != null){
            sql.append(" and create_time <=").append(countAchievementQuery.getEndTime());
        }
        if(countAchievementQuery.getStaffUserId() != null){
            sql.append(" and staff_user_id =").append(countAchievementQuery.getStaffUserId());
        }
        if(countAchievementQuery.getType() != null){
            switch(countAchievementQuery.getType()){
                case "2" :
                    sql.append(" GROUP BY org_id");
                    break;
                case "3" :
                    sql.append(" GROUP BY department_id");
                    break;
                case "4" :
                    sql.append(" GROUP BY provincial_section_id");
                    break;
                default :
                    sql.append(" GROUP BY staff_user_id");
            }
        }
        sql.append(" ORDER BY price desc");
    }

    @Override
    public List<EvaluateDto> evaluateCount(EvaluateCountQuery evaluateCountQuery) throws AdminException {
        List<Org> orgList = orgRepository.findByDeletedFalse();
        List<EvaluateDto> list = new ArrayList<>();
        for(Org org : orgList){
            BigDecimal score = BigDecimal.ZERO;
            List<Map> maps = new ArrayList<>();
            EvaluateDto evaluateDto = new EvaluateDto();
            List<BossUser> bossUsers = bossUserRepository.findByDeletedFalseAndSection(org);
            List<EvaluationTemplate> evaluationTemplateList = evaluationTemplateRepository.findByDeletedFalseAndParentIsNotNull();
            for(BossUser bossUser : bossUsers){
                if(bossUser.getAverageScore() != null) {
                    score = score.add(bossUser.getAverageScore());
                }
            }

            for(EvaluationTemplate evaluationTemplate : evaluationTemplateList){
                Long sumChildScore = 0L;
                Map<String,Object> map = new HashMap<>();
                map.put("title",evaluationTemplate.getName());
                List<EvaluationRecordTemplate> evaluationRecordTemplateList = evaluationRecordTemplateRepository.findByDeletedFalseAndEvaluationTemplateAndEvaluationRecordSubscribeSysDept(evaluationTemplate,org);
                for(EvaluationRecordTemplate evaluationRecordTemplate : evaluationRecordTemplateList){
                    sumChildScore = sumChildScore+evaluationRecordTemplate.getEvaluationTemplateChild().getScore();
                }
                if(sumChildScore != 0 && evaluationRecordTemplateList.size() != 0){
                    map.put("sumChildScore",new BigDecimal(sumChildScore).divide(new BigDecimal(evaluationRecordTemplateList.size()),1,RoundingMode.HALF_UP));
                }else {
                    map.put("sumChildScore",BigDecimal.ZERO);
                }
                maps.add(map);
            }
            BigDecimal averageScore = new BigDecimal("0");
            if(bossUsers.size() > 0) {
                averageScore = score.divide(new BigDecimal(bossUsers.size()),1, RoundingMode.HALF_UP);
            }
            evaluateDto.setAverageScore(averageScore);
            evaluateDto.setOrgId(org.getId());
            evaluateDto.setOrgName(org.getSimpleName());
            evaluateDto.setMaps(maps);
            list.add(evaluateDto);
        }
        Collections.sort(list, new Comparator<EvaluateDto>() {
            public int compare(EvaluateDto u1, EvaluateDto u2) {
                if (u1.getAverageScore().compareTo(u2.getAverageScore()) == 1 ) {
                    return -1;
                }
                if (u1.getAverageScore().compareTo(u2.getAverageScore()) == 0) {
                    return 0;
                }
                return 1;
            }
        });
        return list;
    }

    @Override
    public List<BossUser.FrontBossUser> listByEvaluate(Long orgId) throws AdminException {
        SysSection section = dao.get(SysSection.class,orgId);
        if(section == null){
            throw new AdminException("门店信息异常");
        }
        List<BossUser> bossUsers = bossUserRepository.findByDeletedFalseAndSectionOrderByAverageScoreDesc(section);
        List<BossUser.FrontBossUser> list = new ArrayList<>();
        bossUsers.forEach(bossUser -> {
            list.add(bossUser.toFrontBossUser());
        });
        return list;
    }

    @Override
    public Vo subscribeCount(SubscribeCountQuery subscribeCountQuery) throws AdminException {
        subscribeCountQuery.format();
        Map<String, Object> query = subscribeCountQuery.parseMap();
        if(subscribeCountQuery.getStaffUserId() != null){
            query.put("staffUser_id",subscribeCountQuery.getStaffUserId());
            query.remove("staffUserId");
        }
        if(subscribeCountQuery.getBeginTime() != null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,subscribeCountQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(subscribeCountQuery.getEndTime() != null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,subscribeCountQuery.getEndTime());
            query.remove("endTime");
        }
        List<SubscribeStatistics> subscribeStatistics = dao.find(dao.query2Hql(SubscribeStatistics.class,query),query);
        Long count = dao.count(dao.count2Hql(SubscribeStatistics.class,query),query);
        PageHelper.startPage(subscribeCountQuery.getPage(),subscribeCountQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<SubscribeStatistics.Vo>) VO.toVOCollection(subscribeStatistics));
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }

    @Override
    public List<ServiceProjectStatisticsDto> projectCount(ProjectStatisticsQuery projectStatisticsQuery) throws AdminException {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COALESCE(SUM(consume_price),0) consumePrice,COALESCE(SUM(recharge_price),0) rechargePrice, SUM(subscribe_num) subscribeNum,\n" +
                "org_id orgId,service_project_id serviceProjectId,create_time createTime,price\n" +
                "FROM `service_project_statistics` where deleted=false \n");
        if(projectStatisticsQuery.getOrgId() != null){
            sql.append(" and org_id = ").append(projectStatisticsQuery.getOrgId());
        }
        if(projectStatisticsQuery.getServiceProjectId() != null){
            sql.append(" and service_project_id = ").append(projectStatisticsQuery.getServiceProjectId());
        }
        if(projectStatisticsQuery.getBeginTime() != null){
            sql.append( " and create_time >= ").append(projectStatisticsQuery.getBeginTime());
        }
        if(projectStatisticsQuery.getEndTime() != null){
            sql.append( " and create_time <= ").append(projectStatisticsQuery.getEndTime());
        }
        sql.append(" GROUP BY org_id, service_project_id ");
        sql.append(" ORDER BY price desc");
        Map<String, Object> query = new HashMap<>(16);
        List<Map> list = dao.findBySql2Map(sql.toString(), query);
        List<ServiceProjectStatisticsDto> serviceProjectStatisticsList = new ArrayList<>();
        for(Map m : list){
            ServiceProjectStatisticsDto serviceProjectStatistics = new ServiceProjectStatisticsDto();
            BigDecimal rechargePrice = BigDecimal.ZERO;
            BigDecimal consumePrice = BigDecimal.ZERO;
            Long subscribeNum = 0L;
            if(m.get("rechargePrice") != null && StringUtils.isNotBlank(m.get("rechargePrice").toString())){
                rechargePrice = (BigDecimal) m.get("rechargePrice");
            }
            if(m.get("consumePrice") != null && StringUtils.isNotBlank(m.get("consumePrice").toString())){
                consumePrice = (BigDecimal) m.get("consumePrice");
            }
            if(m.get("subscribeNum") != null && StringUtils.isNotBlank(m.get("subscribeNum").toString())){
                subscribeNum = Long.parseLong( m.get("subscribeNum").toString());
            }
            if(m.get("price") != null && StringUtils.isNotBlank(m.get("price").toString())){
                serviceProjectStatistics.setPrice((BigDecimal) m.get("price"));
            }
            serviceProjectStatistics.setSubscribeNum(subscribeNum);
            serviceProjectStatistics.setRechargePrice(rechargePrice);
            serviceProjectStatistics.setConsumePrice(consumePrice);
            if(m.get("orgId") != null && StringUtils.isNotBlank(m.get("orgId").toString())){
                serviceProjectStatistics.setOrgId(Long.parseLong( m.get("orgId").toString()));
                SysSection section = dao.get(SysSection.class,Long.parseLong( m.get("orgId").toString()));
                if(section != null){
                    serviceProjectStatistics.setOrgName(section.getSimpleName());
                }
            }
            if(m.get("serviceProjectId") != null && StringUtils.isNotBlank(m.get("serviceProjectId").toString())){
                serviceProjectStatistics.setServiceProjectId(Long.parseLong( m.get("serviceProjectId").toString()));
                ServiceProject serviceProject = dao.get(ServiceProject.class,Long.parseLong( m.get("serviceProjectId").toString()));
                if(serviceProject != null){
                    serviceProjectStatistics.setProjectName(serviceProject.getProjectName());
                }
            }
            if(m.get("createTime") != null && StringUtils.isNotBlank(m.get("createTime").toString())){
                serviceProjectStatistics.setCreateTime(TimeUtil.longToString(Long.parseLong(m.get("createTime").toString()),TimeUtil.MMDD));
            }
            serviceProjectStatisticsList.add(serviceProjectStatistics);
        }
        return serviceProjectStatisticsList;
    }

    @Override
    public List<IncomingStatisticsDto> countIncomingStatistics(IncomingStatisticsQuery incomingStatisticsQuery) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COALESCE(sum(o.price),0) price ,ss.simple_name orgName FROM `orders` o LEFT JOIN sys_section ss on o.org_id = ss.id\n" +
                "where o.dtype='OrgOrder' and o.deleted=0 and o.org_order_status =4 ");
        if(incomingStatisticsQuery.getOrgId() != null){
            sql.append(" and o.org_id = ").append(incomingStatisticsQuery.getOrgId());
        }
        if(incomingStatisticsQuery.getBeginTime() != null){
            sql.append(" and o.createTime >= ").append(incomingStatisticsQuery.getBeginTime());
        }
        if(incomingStatisticsQuery.getEndTime() != null){
            sql.append(" and o.createTime <= ").append(incomingStatisticsQuery.getEndTime());
        }
        sql.append(" GROUP BY o.org_id");
        sql.append(" order by price desc");

        System.out.println(sql);

        Map<String, Object> query = new HashMap<>(16);
        if (incomingStatisticsQuery.getPage() != null && incomingStatisticsQuery.getSize() != null) {
            query.put("size", incomingStatisticsQuery.getSize());
            query.put("page", incomingStatisticsQuery.getPage());
        }
        List<Map> list = dao.findBySql2Map(sql.toString(), query);

        List<IncomingStatisticsDto> incomingStatisticsDtoList = new ArrayList<>();
        for (Map m : list){
            IncomingStatisticsDto incomingStatisticsDto = new IncomingStatisticsDto();
            incomingStatisticsDto.setSum((BigDecimal) m.get("price"));
            incomingStatisticsDto.setOrgName(m.get("orgName").toString());
            incomingStatisticsDtoList.add(incomingStatisticsDto);
        }
        return incomingStatisticsDtoList;
    }


}
